<template>
  <el-dialog width="1000px" destroy-on-close append-to-body :visible.sync="show" :before-close="cancel">
    <template #title>
      <div>
        <b>{{ formData.name }}</b> 自派送批次,<span>{{ $t('选择包裹导入') }}</span>
      </div>
    </template>
    <el-row>
      <el-form :model="queryParams" ref="queryForm" size="small" inline v-show="showSearch">
        <el-form-item prop="addressList">
          <el-select
            style="width: 300px"
            clearable
            filterable
            @change="getList"
            :placeholder="$t('地区')"
            multiple
            v-model="queryParams.addressList"
          >
            <el-option v-for="item in cityList" :key="item.id" :label="item.name" :value="item.name" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="getList">
            {{ $t('搜索') }}
          </el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
            {{ $t('重置') }}
          </el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <el-row style="margin-bottom: 10px">
      <el-button
        type="primary"
        :disabled="!tableCheck.length"
        size="mini"
        v-hasPermi="['distribution:googleRouteBatch:chooseOrderBatchImport']"
        @click="handleImport"
      >
        {{ $t('选择导入') }}
      </el-button>
    </el-row>
    <el-table
      :height="calTableHeight() - 100"
      ref="TableRef"
      :data="tableData"
      @selection-change="(rows) => (tableCheck = rows)"
    >
      <!-- 租户 店铺名称/订单平台 创建时间 已加入自派送数量 当前未发货包裹数 -->
      <el-table-column :selectable="() => true" type="selection" width="55" align="center" />
      <el-table-column show-overflow-tooltip align="center" :label="$t('租户')" prop="distrName" />
      <el-table-column
        show-overflow-tooltip
        align="center"
        :label="$t('店铺名称/订单平台')"
        prop="shopName"
        width="150"
      >
        <template #default="{ row }">
          <div>{{ row.shopName }}</div>
          <div>{{ handleType(row.shopType) }}</div>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip align="center" :label="$t('创建时间')" prop="createTime" />
      <el-table-column show-overflow-tooltip align="center" :label="$t('已加入自派送数量')" prop="selfDeliveryNum" />
      <el-table-column
        show-overflow-tooltip
        align="center"
        :label="$t('当前未发货包裹数')"
        prop="currentUnDeliveryNum"
      />
    </el-table>
    <div slot="footer">
      <!-- <el-button
        v-hasPermi="['distribution:googleRouteBatch:chooseOrderBatchImport']"
        @click="handleSubmit"
        type="primary"
      >
        {{ $t('确定') }}
      </el-button> -->
      <el-button @click="cancel">
        {{ $t('关闭') }}
      </el-button>
    </div>
  </el-dialog>
</template>
<script>
// import { api } from '@/api/**'
import {
  googleRouteBatchChooseOrderBatch,
  cityList,
  googleRouteBatchChooseOrderBatchImport,
} from '@/api/orderManage/batchDispatch'

export default {
  props: {
    /**
     * formData
     */
    formData: {
      type: Object,
      default: () => ({}),
    },
    /**
     * type
     */
    type: {
      type: String,
    },
    /**
     * 控制显示隐藏
     */
    show: {
      type: Boolean,
      required: true,
      default: false,
    },
  },
  data() {
    return {
      shop_type_list: [],
      cityList: [],
      /**显示搜索条件 */
      showSearch: true,
      /**表格选中项 */
      tableCheck: [],
      /**表格数据 */
      tableData: [],
      loading: false,
      /**总条数 */
      total: 0,
      /**分页相关查询参数 */
      queryParams: {
        addressList: [],
      },
    }
  },
  computed: {},
  watch: {
    show: function (newValue) {
      if (newValue) {
        this.getList()
        this.useDict('shop_type').then(({ shop_type }) => {
          this.shop_type_list = shop_type
        })
        cityList().then((res) => {
          this.cityList = res.data
        })
      }
    },
  },
  methods: {
    handleType(type) {
      const index = this.shop_type_list.findIndex((i) => i.dictValue == type)
      return this.shop_type_list[index]?.dictLabel
    },
    handleSubmit() {
      this.cancel()
    },
    handleImport() {
      this.$confirm(this.$t('确认导入?'), this.$t('提示'), {
        type: 'warning',
      }).then(() => {
        const loading = this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.5)',
        })
        const data = {
          id: this.formData.id,
          orderBatchIdList: this.tableCheck.map((i) => i.orderBatchId),
          addressList: this.queryParams.addressList,
        }
        googleRouteBatchChooseOrderBatchImport(data)
          .then((res) => {
            const { code, data, msg } = res
            if (code === 200) {
              this.$message({ type: 'success', message: msg })
              this.cancel()
              this.$emit('getList')
            }
          })
          .finally(() => loading.close())
      })
    },
    getList() {
      this.loading = true
      googleRouteBatchChooseOrderBatch({
        ...this.queryParams,
        id: this.formData.id,
      })
        .then((res) => {
          this.tableData = res.data
        })
        .finally(() => (this.loading = false))
    },

    cancel() {
      this.$emit('getList')
      this.$emit('update:show', false)
    },
    /** 重置按钮操作 */
    resetQuery() {
      Object.keys(this.queryParams).forEach((key) => (this.queryParams[key] = null))
      this.queryParams.addressList = []
      this.getList()
    },
  },
}
</script>
<style scoped lang="scss"></style>
